package com.t9101DR.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import com.t9101DR.bean.RoomAddress;
import com.t9101DR.service.RoomAddressService;
import com.t9101DR.service.impl.RoomAddressServiceImpl;

/**
 * Servlet implementation class RoomAddressServiet
 */
@WebServlet("/RoomAddressServlet")
public class RoomAddressServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoomAddressServlet() {
        super();
        // TOD  O Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		//action是请求的操作类别
		String action=request.getParameter("action");
		RoomAddress stu=new RoomAddress();
		
		RoomAddressService newsService=new RoomAddressServiceImpl(); 
		if(action==null || "list".equals(action)) {
			//从请求中拿到数据
				String newsTitle = request.getParameter("name");
				if(newsTitle!=""&&newsTitle!=null) {
					
						stu.setAddressId(Integer.parseInt(newsTitle));
					
				}
				
				RoomAddress news=new RoomAddress();
				//调用业务逻辑层的方法
				List<RoomAddress> list=newsService.list(stu);
				//获取分页数据
				//页面的跳转和数据的设备
				Map<String, Object>map=new HashMap<>();
				if (list!=null) {
				map.put("code", 0);
				map.put("success", true);
				map.put("count", 100);
			}else {
				map.put("code", -1);
				map.put("success", false);
			}
			request.setAttribute("listSize", list.size());
			String result=JSON.toJSONString(map);
			PrintWriter pw=response.getWriter();
			pw.write(result);
			pw.flush();
			pw.close();
			}
			

		if("update".equals(action)) {
			int nid=Integer.parseInt(request.getParameter("nid"));;
			RoomAddress n =new RoomAddress();
			n.setAddressId(nid);
			List<RoomAddress> list=newsService.list(n);
			if(list!=null&&list.size()>0) {
				request.setAttribute("news", list.get(0));
				
			}
			request.getRequestDispatcher("tgls/goodsManage/goods_update.jsp").forward(request, response);
			
		}
		if("cupdate".equals(action)) {			
			//从请求中拿到数据
			int tid=Integer.parseInt(request.getParameter("nid"));
			String newsContext=	request.getParameter("newsContext");
			String createDate=	request.getParameter("createDate");
			String dizhi=request.getParameter("dizhi");
			int  eami=Integer.parseInt(request.getParameter("eamil"));
			boolean  dui;
			if(request.getParameter("sfpfsp").equals("true")) {
				 dui=true;
			}else {
				dui=false;
			}		
			//根据请求中的数据调用业务逻辑层的方法
			RoomAddress n1 =new RoomAddress();
			n1.setAddressId(tid);
			n1.setAddName(newsContext);
			n1.setAddArea(createDate);
			n1.setAdd(dizhi);
			n1.setPhone(eami);
			n1.setDefaultAdd(dui);
		    int count=newsService.update(n1);
			//页面的跳转和数据的设置		
			Map<String, Object> map=new HashMap<>();
			if(count>0) {
				
				map.put("code",0);
				map.put("msg","修改成功");
				
			}else {
				
				map.put("code",-1);
				map.put("msg","失败");
				
			}
			String	result=	JSON.toJSONString(map);
			PrintWriter pw =response.getWriter();
			pw.write(result);
			pw.flush();
			pw.close();	
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
